
#include <string.h>

#include "core/olisp_mem.h"

#ifdef OLISP_DBG_MEM_IMPL

void * lisp_mallocz_impl( size_t s, const char * file, int line )
{
    void * p = _malloc_dbg( s, _NORMAL_BLOCK, file, line );
    if( p ) memset( p, 0, s );
    return p;
}

char * lisp_strdup_impl( const char * s, const char * file, int line )
{
    size_t len = strlen( s ) + 1;
    char * r = _malloc_dbg( len, _NORMAL_BLOCK, file, line );
    
    if( r )
        memcpy( r, s, len );
    
    return r;
}

void lisp_dump_mem_leaks()
{
    _CrtDumpMemoryLeaks();
}

#else

#include <stdlib.h>

void * lisp_malloc( size_t s )
{
    return malloc( s );
}

void lisp_free( void * p )
{
    free( p );
}

void * lisp_realloc( void * p, size_t s )
{
    return realloc( p, s );
}

void * lisp_mallocz( size_t s )
{
    void * p = lisp_malloc( s );
    if( p ) memset( p, 0, s );
    return p;
}

char * lisp_strdup( const char * s )
{
    size_t len = strlen( s ) + 1;
    char * r = lisp_malloc( len );
    
    if( r )
        memcpy( r, s, len );
    
    return r;
}

#endif // OLISP_DBG_MEM_IMPL
